<!DOCTYPE html>
<html>
<head lang="zh-cmn-Hans">
    <meta charset="UTF-8">
    <title>CORS</title>
</head>
<script type="text/javascript">

    function createCORSRequest(method, url) {
        var xhr = new XMLHttpRequest();
        if ("withCredentials" in xhr) {
            // 此时即支持CORS的情况
            // 检查XMLHttpRequest对象是否有“withCredentials”属性
            // “withCredentials”仅存在于XMLHTTPRequest2对象里
            xhr.open(method, url, true);
        } else if (typeof XDomainRequest != "undefined") {
            // 否则检查是否支持XDomainRequest，IE8和IE9支持
            // XDomainRequest仅存在于IE中，是IE用于支持CORS请求的方式
            xhr = new XDomainRequest();
            xhr.open(method, url);
        } else {
            // 否则，浏览器不支持CORS
            xhr = null;
        }
        return xhr;
    }

    var xhr = createCORSRequest('GET', "http://service.ibruce.info/nocors");
    if (!xhr) throw new Error('CORS not supported');

    //    xhr.setRequestHeader("accept", "application/vnd.busuanzi.v8+json,*/*");

    //    xhr.onload = function (e) {
    //        // var data = JSON.parse(this.response);
    //        alert(this.response);
    //    }

    //    alert(xhr.responseText);
    xhr.onload = function (e) {
        if (this.status == 200)
            alert("1" + this.responseText);
        else
            alert("2" + this.status);
    };

    xhr.onreadystatechange = function () {

        if (xhr.readyState == 4 && xhr.status == 0)
            alert("3" + xhr.responseText);
        else
            alert("4" + this.status);
    }
    xhr.send();

    //    alert(xhr);
</script>
<body>


</body>
</html>